package mobidever.godutch.business;

import java.util.ArrayList;
import java.util.List;

import mobidever.godutch.business.base.BusinessBase;
import mobidever.godutch.database.sqlitedal.SQLiteDALAccountBook;
import mobidever.godutch.model.ModelAccountBook;
import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

public class BusinessAccountBook extends BusinessBase {
	private SQLiteDALAccountBook mSqLiteDALAccountBook;

	public BusinessAccountBook(Context pContext) {
		super(pContext);
		mSqLiteDALAccountBook = new SQLiteDALAccountBook(pContext);
	}

	public boolean InsertAccountBook(ModelAccountBook pInfo) {
		mSqLiteDALAccountBook.beginTransaction();
		try {
			boolean _Result = mSqLiteDALAccountBook.InsertAccountBook(pInfo);
			boolean _Result2 = true;
			if (pInfo.getmIsDefault() == 1 && _Result) {
				_Result2 = setIsDefault(pInfo.getmAccountBookID());
			}
			if (_Result && _Result2) {
				mSqLiteDALAccountBook.setTransactionSuccessful();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			mSqLiteDALAccountBook.endTransaction();
		}
	}

	public boolean DeleteAccountBookByAccountBookID(int pAccountBookID) {
		mSqLiteDALAccountBook.beginTransaction();
		try {
			String _Condition = " And AccountBookID = " + pAccountBookID;
			Boolean _Result = mSqLiteDALAccountBook
					.DeleteAccountBook(_Condition);
			Boolean _Result2 = true;
			if (_Result) {
				BusinessPayout _BusinessPayout = new BusinessPayout(
						getContext());
				_Result2 = _BusinessPayout
						.DeletePayoutByAccountBookID(pAccountBookID);
			}

			if (_Result && _Result2) {
				mSqLiteDALAccountBook.setTransactionSuccessful();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			mSqLiteDALAccountBook.endTransaction();
		}
	}

	public boolean UpdateAccountBookByAccountBookID(
			ModelAccountBook pModelAccountBook) {
		mSqLiteDALAccountBook.beginTransaction();
		try {
			String _Condition = " AccountBookID = " + pModelAccountBook.getmAccountBookID();
			Boolean _Result = mSqLiteDALAccountBook.UpdateAccountBook(
					_Condition, pModelAccountBook);
			Boolean _Result2 = true;
			if (pModelAccountBook.getmIsDefault() == 1 && _Result) {
				_Result2 = setIsDefault(pModelAccountBook.getmAccountBookID());
			}

			if (_Result && _Result2) {
				mSqLiteDALAccountBook.setTransactionSuccessful();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			mSqLiteDALAccountBook.endTransaction();
		}
	}

	public List<ModelAccountBook> getAccountBooks(String pCondition) {
		return mSqLiteDALAccountBook.getAccountBooks(pCondition);
	}

	public ModelAccountBook getModelAccountBookByAccountBookID(
			int pAccountBookID) {
		List<ModelAccountBook> _List = mSqLiteDALAccountBook
				.getAccountBooks(" And AccountBookID = " + pAccountBookID);
		if (_List.size() == 1) {
			return _List.get(0);
		} else {
			return null;
		}
	}

	public List<ModelAccountBook> getAccountBookListByAccountBookID(
			String[] pAccountBookID) {
		List<ModelAccountBook> _List = new ArrayList<ModelAccountBook>();
		for (int i = 0; i < pAccountBookID.length; i++) {
			_List.add(getModelAccountBookByAccountBookID(Integer
					.valueOf(pAccountBookID[1])));
		}
		return _List;
	}

	public ModelAccountBook GetDefaultModelAccountBook() {
		List _List = mSqLiteDALAccountBook
				.getAccountBooks(" IsDefault = 1");
		if (_List.size() == 1) {
			return (ModelAccountBook) _List.get(0);
		} else {
			return null;
		}
	}

	public boolean IsExistAccountBookByAccountBookName(String pAccountBookName,
			Integer pAccountBookID) {
		String _Condtion = " AccountBookName = '" + pAccountBookName + "'";
		if (pAccountBookID != null) {
			_Condtion += " And AccountBookID <> " + pAccountBookID;
		}
		List _List = mSqLiteDALAccountBook.getAccountBooks(_Condtion);
		if (_List.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	

	public boolean setIsDefault(int pID) throws Exception {
		String _Condition = " IsDefault = 1";
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("IsDefault", 0);
		boolean _Result = mSqLiteDALAccountBook.UpdateAccountBook(_Condition,
				_ContentValues);
		_Condition = " AccountBookID = " + pID;
		_ContentValues.clear();
		_ContentValues.put("IsDefault", 1);
		boolean _Result2 = mSqLiteDALAccountBook.UpdateAccountBook(_Condition,
				_ContentValues);
		if (_Result && _Result2) {
			return true;
		} else {
			return false;
		}
	}
	public String GetAccountBookNameByAccountId(int p_BookId) {
		String _ConditionString = " AccountBookID = " + String.valueOf(p_BookId);
		List<ModelAccountBook> _AccountBooks = mSqLiteDALAccountBook.getAccountBooks(_ConditionString);
		return _AccountBooks.get(0).getmAccountBookName();
	}
}
